package com.goods.lucene.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.common.domain.GoodsVo;
import com.common.query.QueryResult;
import com.common.resource.GoodsResource;

/**
 * 索引管理类 读取，写入
 * 
 * @author penghui.li 2012-06-06
 * @param <T>
 */
@SuppressWarnings({ "static-access", "rawtypes" })
public class IndexManage<T> {

	private final static String LUCENE_PATH = "d:/opt/lucene/index";

	/**
	 * 创建索引文件
	 * 
	 * @param list
	 */
	public static void create(List<GoodsVo> list) throws Exception {
		Directory fsDir = FSDirectory.open(new File(LUCENE_PATH));
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35,
				new IKAnalyzer());
		IndexWriter writer = new IndexWriter(fsDir, config);
		for (GoodsVo vo : list) {
			Document doc = DocumentTool.findObjToDocByObj(vo);
			writer.addDocument(doc);
		}
		// 优化
		writer.forceMerge(10);
		writer.commit();
		writer.close();
	}

	
	public static void main(String[] args) throws Throwable {
		try {
			BooleanQuery booleanMainQuery = new BooleanQuery();
			String queryStr = GoodsResource.FIELD_GOODS_NAME + ":" + "美";
			booleanMainQuery.add(FclubBooleanQuery.getBooleanQuery(queryStr,
					GoodsResource.FIELD_GOODS_NAME), BooleanClause.Occur.MUST);
			new IndexManage().search(booleanMainQuery,0,9);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* search index */
	public static QueryResult<GoodsVo> search(Query query,int startNo,int endNo) throws Throwable {
		QueryResult<GoodsVo> result = new QueryResult<GoodsVo>();
		IndexSearcher searcher = null;
		TopDocs topDocs = null;
		List<GoodsVo> list = null;
		try {
			Directory directory = FSDirectory.open(new File(LUCENE_PATH));
			// 判断目录是否是lucene可读的索引目录
			boolean exists = IndexReader.indexExists(directory);
			if (exists) {
				IndexReader reader = IndexReader.open(directory);
				searcher = new IndexSearcher(reader);
			}
			topDocs = searcher.search(query, 1000000);
			int num = topDocs.scoreDocs.length;
			list = execute(searcher, topDocs, startNo, endNo);
			result.setTotal(num);
			result.setResult(list);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static List<GoodsVo> execute(IndexSearcher searcher, TopDocs topDocs,
			int startNO, int endNO) {
		List<GoodsVo> list = new ArrayList<GoodsVo>();
		if(endNO>topDocs.scoreDocs.length){
			endNO=topDocs.scoreDocs.length;
		}
		try {
			for (int i = startNO; i < endNO; i++) {
				int docId = topDocs.scoreDocs[i].doc;
				Document doc = searcher.doc(docId);
				list.add(DocumentTool.findDoc2ObjectByDoc(doc));
			}
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	

	public List<GoodsVo> read() {
		return null;
	}

}
